/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller.upload;

import com.mysql.jdbc.Blob;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.io.*;
import java.net.URLDecoder;
import java.sql.CallableStatement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.jms.Session;
import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import queenb.net.entitybean.K100;
import queenb.net.util.QueenbUtil;
import queenb.web.crm.store.K100Store;
import queenb.web.session.LoginSession;
import queenb.web.util.SessionProvider;

/**
 *
 * @author VANANH
 */
public class image extends HttpServlet {

    private static final int DEFAULT_BUFFER_SIZE = 1024;
    private String imagePath;

    // Actions ------------------------------------------------------------------------------------
    public void init() throws ServletException {

        // Define base path somehow. You can define it as init-param of the servlet.
        this.imagePath = "D:/kapi";

        // In a Windows environment with the Applicationserver running on the
        // c: volume, the above path is exactly the same as "c:\images".
        // In UNIX, it is just straightforward "/images".
        // If you have stored files in the WebContent of a WAR, for example in the
        // "/WEB-INF/images" folder, then you can retrieve the absolute path by:
        // this.imagePath = getServletContext().getRealPath("/WEB-INF/images");
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request8
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("id");
        Object pk100 = request.getParameter("pk100");
        int id1 = Integer.parseInt(id);
        String pk100T = pk100.toString();
        Statement stmt = null;
        ResultSet rs = null;
        SessionFactory sff = null;
        try {

            ServletOutputStream out = response.getOutputStream();
            sff = QueenbUtil.getSessionFactory();

            stmt = (Statement) sff.openSession().connection().createStatement();
            String cc = "SELECT kb113 FROM " + pk100T + " WHERE  PK100 = '" + id1 + "' ";
            rs = stmt.executeQuery(cc);
            if (rs.next()) {
                byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
                buffer = rs.getBytes("kb113");
                if (buffer != null) {
                    response.setContentType("image/jpg");
                    response.getOutputStream().write(buffer, 0, rs.getBytes("kb113").length);
                }
                response.getOutputStream().flush();
                response.getOutputStream().close();

            }
        } catch (SQLException ex) {
            Logger.getLogger(image.class.getName()).log(Level.SEVERE, null, ex);
            sff.close();
        } finally {
            sff.close();
        }




    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
